package com.chen.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chen.common.ValidationEnum;
import com.chen.entity.Validation;
import com.chen.mapper.ValidationMapper;
import com.chen.service.IValidationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author personcoder007
 * @since 2022-08-01
 */
@Service
public class ValidationServiceImpl extends ServiceImpl<ValidationMapper, Validation> implements IValidationService {

    @Autowired
    private ValidationMapper validationMapper;

    @Override
    @Transactional
    public void saveCode(String email, String code, Integer type, DateTime expire) {
        Validation validation = new Validation();
        validation.setEmail(email);
        validation.setCode(code);
        validation.setType(type);
        validation.setTime(expire);
//        删除同类型的验证
        UpdateWrapper<Validation> validationUpdateWrapper = new UpdateWrapper<>();
        validationUpdateWrapper.eq("email", email);
        validationUpdateWrapper.eq("type", ValidationEnum.LOGIN.getCode());
        remove(validationUpdateWrapper);

        save(validation);

    }
}
